package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.*;
import com.dongdongshop.admin.vo.AreaVO;
import com.dongdongshop.admin.vo.CitiesVO;
import com.dongdongshop.admin.vo.ProvincesVO;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.service.ProvincesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProvincesServiceImpl implements ProvincesService {
    @Autowired
    private TbProvincesMapper provincesMapper;
    @Autowired
    private TbCitiesMapper citiesMapper;
    @Autowired
    private TbAreasMapper areasMapper;

    @Override
    public List<ProvincesVO> getByprovinceid() {
        List<TbProvinces> tbProvinces = provincesMapper.selectByExample(null);
        return tbProvinces.stream().map(m->{
            ProvincesVO p  = new ProvincesVO();
            BeanUtils.copyProperties(m, p);
            return p;
        }).collect(Collectors.toList());

    }

    @Override
    public List<CitiesVO> getBycitiesid(String id) {
        TbCitiesExample example = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceidEqualTo(id);
        List<TbCities> tbCities = citiesMapper.selectByExample(example);
        return tbCities.stream().map(c->{
            CitiesVO citiesVO = new CitiesVO();
            BeanUtils.copyProperties(c, citiesVO);
            return citiesVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AreaVO> getByareaid(String id) {
        TbAreasExample e = new TbAreasExample();
        TbAreasExample.Criteria criteria = e.createCriteria();
        criteria.andCityidEqualTo(id);
        List<TbAreas> tbAreas = areasMapper.selectByExample(e);
        return tbAreas.stream().map(s->{
            AreaVO vo = new AreaVO();
            BeanUtils.copyProperties(s, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
